home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / lib / calyearstart.c.z / calyearstart.c
C/C++ Source or Header  |  1998-01-21  |  1KB  |  63 lines

  1. /*
  2.  * calyearstart - determine the NTP time at midnight of January 1 in
  3.  *          the year of the given date.
  4.  */
  5. #include <sys/types.h>
  6.  
  7. #include "ntp_types.h"
  8. #include "ntp_calendar.h"
  9. #include "ntp_stdlib.h"
  10.  
  11. /*
  12.  * calyeartab - year start offsets from the beginning of a cycle
  13.  */
  14. u_long calyeartab[YEARSPERCYCLE] = {
  15.     (SECSPERLEAPYEAR-JANFEBLEAP),
  16.     (SECSPERLEAPYEAR-JANFEBLEAP) + SECSPERYEAR,
  17.     (SECSPERLEAPYEAR-JANFEBLEAP) + 2*SECSPERYEAR,
  18.     (SECSPERLEAPYEAR-JANFEBLEAP) + 3*SECSPERYEAR
  19. };
  20.  
  21. u_long
  22. calyearstart(dateinyear)
  23.     register u_long dateinyear;
  24. {
  25.     register u_long cyclestart;
  26.     register u_long nextyear, lastyear;
  27.     register int i;
  28.  
  29.     /*
  30.      * Find the start of the cycle this is in.
  31.      */
  32.     if (dateinyear >= MAR1988)
  33.         cyclestart = MAR1988;
  34.     else
  35.         cyclestart = MAR1900;
  36.     while ((cyclestart + SECSPERCYCLE) <= dateinyear)
  37.         cyclestart += SECSPERCYCLE;
  38.     
  39.     /*
  40.      * If we're in the first year of the cycle, January 1 is
  41.      * two months back from the cyclestart and the year is
  42.      * a leap year.
  43.      */
  44.     lastyear = cyclestart + calyeartab[0];
  45.     if (dateinyear < lastyear)
  46.         return (cyclestart - JANFEBLEAP);
  47.  
  48.     /*
  49.      * Look for an intermediate year
  50.      */
  51.     for (i = 1; i < YEARSPERCYCLE; i++) {
  52.         nextyear = cyclestart + calyeartab[i];
  53.         if (dateinyear < nextyear)
  54.             return lastyear;
  55.         lastyear = nextyear;
  56.     }
  57.  
  58.     /*
  59.      * Not found, must be in last two months of cycle
  60.      */
  61.     return nextyear;
  62. }
  63.